'use strict'

const http=require('http');
const fs=require('fs');

//在浏览器输入:localhost:3456/upload

function parseBodyHeaders(data){
    let headers={}
    let dsplit=data.split('\r\n').filter(p=>p.length>0)

    let ind;
    let k
    for(let d of dsplit){
        ind=d.indexOf(':')
        if(ind<=0)continue
        
        k=d.substring(0,ind).toLowerCase()

        headers[k]=d.substring(ind+1).trim()//trim把空格全去掉

    }

    let cpd=headers['content-disposition']

    //按照;分割成数组，第一个元素form-data去掉
    let clist=cpd.split('; ').slice(1)

    let name,filename
    
    for(let a of clist){
        ind=a.indexOf('filename="')
        if(ind>=0){
            filename=a.substring(ind+10,a.length-1)//a.length-1:去掉最后一个"
        }else{
            name=a.substring(6,a.length-1)
        }
    }

    return{
        headers,
        name,
        filename
    }
}

/**
 * 
 * @param {Buffer} bodyData 原始的body数据
 * @param {Object} reqHeaders request.headers
 */

function parseBody(bodyData,reqHeaders){

    //content-type:multipart/form-data;boundary=-----34294329
    let ctype=reqHeaders['content-type']

    //拿到分界线boundary
    let bdy=ctype.substring(ctype.indexOf('=')+1)

    //数据中用于分割文件的分界线，前面的'--'是body解析时自动加上的，'\r\n'是换行

    let crlf_bdy=`\r\n--${bdy}`

    //从偏移bdy的长度开始查找下一个分界线的位置
    let data_end_index=bodyData.indexOf(crlf_bdy,crlf_bdy.length)

    //body消息数据中header部分的最后索引
    let header_end_index=bodyData.indexOf('\r\n\r\n',crlf_bdy.length)

    let header_data=bodyData.toString('utf-8',
                                        crlf_bdy.length,
                                        header_end_index)//结束位置

    //解析body数据中的消息头
    let hd=parseBodyHeaders(header_data)

    let fileinfo={
        start:header_end_index+4,//除去\r\n\r\n
        end:data_end_index,
    }
    fileinfo.length=fileinfo.end-fileinfo.start

    return{
        name:hd.name,
        filename:hd.filename,
        headers:hd.headers,
        start:fileinfo.start,
        end:fileinfo.end,
        length:fileinfo.length
    }
}



let routerTable={

    GET:{//GET /upload HTTP/1.1获取资源
        '/upload':async(request,response)=>{
            console.log('get')
            let stm=fs.createReadStream('./upload.html')
            stm.pipe(response)
            stm.on('end',()=>{
                response.end()
            })
            console.log('get')
        }
    },

    POST:{//POST /upload HTTP/1.1创建资源
        '/upload':async(request,response)=>{
            console.log('post')
            console.log(request.headers['content-type'])

            let bufferList=[];
            let totalLength=0;

            request.on('data',chunk=>{
                totalLength+=chunk.length
                bufferList.push(chunk)
            })

            let bodyData=null

            request.on('end',()=>{
                bodyData=Buffer.concat(bufferList,totalLength)
                bufferList=[]

                //解析bodyData数据
                let body=parseBody(bodyData,request.headers)
                
                console.log(body)

                response.setHeader('content-type','text/plain;charset=utf-8')

                response.end(bodyData)
            })
        }
    }
}

let serv=http.createServer()

serv.on('request',(request,response)=>{
    let  rm=routerTable[request.method]

    //根据？切割字符串
    let usplit=request.url.split("?")

    let pathname=usplit[0]
    let querystring=usplit[1] || ''//如果usplit[1]不存在，则为空

    if(!rm || !rm[pathname]){
        response.statusCode=404
        response.end('page not found')
        return
    }

    rm[pathname](request,response)
})

serv.listen(3456)